﻿using ASmile.Com;
using ASmile.Win.Controls;
using ASmile.Win.DaBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASmile.Extend;
using ASmile.Helpers;

namespace ASmile.Win.Extend
{
    public class FormCrudEx<TSaveModel> : FormCrudEx<ArgPageData, TSaveModel, TSaveModel>
        where TSaveModel : class, new()
    { }

    public class FormCrudEx<TArgPageData, TSaveModel> : FormCrudEx<TArgPageData, TSaveModel, TSaveModel>
        where TSaveModel : class, new()
        where TArgPageData : ArgPageData, new()
    { }
    /// <summary>
    /// 增删查改快速实现
    /// </summary>
    public class FormCrudEx<TArgPageData, TSaveModel, TListModel> : FormQuery<TArgPageData, TListModel>
        where TListModel : class, new()
        where TSaveModel : class, new()
        where TArgPageData : ArgPageData, new()
    {
        public IBLLCrud<TArgPageData, TSaveModel, TListModel> BLLObj { get; set; }

        //public FrmBase Owner { set; get; }

        public ToolStripButton BtnAdd { set; get; }

        public ToolStripButton BtnAddCopy { set; get; }

        public ToolStripButton BtnEdit { set; get; }

        public ToolStripButton BtnDel { set; get; }

        public Func<FrmBaseEdit> CreateAddForm { set; get; }

        public Func<FrmBaseEdit> CreateEditForm { set; get; }

        public override void Init()
        {
            GetListHandle = BLLObj.GetList;
            base.Init();
            if (BtnAdd != null) BtnAdd.Click += BtnAdd_Click;
            if (BtnAddCopy != null) BtnAddCopy.Click += BtnCopyAdd_Click;
            if (BtnEdit != null) BtnEdit.Click += BtnEdit_Click;
            if (BtnDel != null) BtnDel.Click += BtnDelete_Click;
            if (Dgv != null) Dgv.CellDoubleClick += Dgv_DoubleClick;
            if (CreateAddForm == null)
            {
                CreateAddForm = CreateEditForm;
            }
            if (CreateEditForm == null)
            {
                CreateEditForm = CreateAddForm;
            }
        }

        void ShowAdd(bool isCopyAdd = false)
        {
            var saveModel = new TSaveModel();
            EditFormArgs arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = m => BLLObj.Add(saveModel),
                SaveModel = saveModel
            };

            if (isCopyAdd)
            {
                arg.SetModel = Dgv.GetSelectedClassData<TListModel>();
                if (arg.SetModel == null)
                {
                    Owner.ShowMessage("没有选中任何行");
                    return;
                }
            }
            var frm = CreateAddForm();
            frm.Text += " 新增";
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                Owner.ShowMessage(ret.msg);
                ReflectionHelper.SetPropertyValue(arg.SaveModel, "Id", ret.data);
                Dgv.AddDataSource(arg.SaveModel);
            }
        }

        public void BtnAdd_Click(object sender, EventArgs e)
        {
            ShowAdd();
        }

        public void BtnCopyAdd_Click(object sender, EventArgs e)
        {
            ShowAdd(true);
        }

        public void BtnEdit_Click(object sender, EventArgs e)
        {
            var selModel = Dgv.GetSelectedClassData<TListModel>();
            if (selModel == null)
            {
                Owner.ShowMessage("没有选中任何行");
                return;
            }

            var frm = CreateEditForm();
            frm.Text += " 编辑";
            if (!(selModel is TSaveModel saveModel))
            {
                saveModel = new TSaveModel();
            }
            EditFormArgs arg = new EditFormArgs
            {
                SaveHandle = m => BLLObj.Update(saveModel),
                SaveModel = saveModel, //这里用视图的model提交服务器理论上没有问题 2021.1.19
                SetModel = selModel,
                IsEditShow = true
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                Dgv.Refresh();
                //BindDataSource();
                Owner.ShowMessage(ret.msg);
            }
        }

        public void BtnDelete_Click(object sender, EventArgs e)
        {
            if (!Owner.ShowQuestion("确实要删除当前记录吗？", "删除确认"))
            {
                return;
            }
            var model = Dgv.GetSelectedClassData<TListModel>();
            if (model == null)
            {
                Owner.ShowMessage("没有选中任何行");
                return;
            }
            if (!(model is TSaveModel m))
            {
                m = new TSaveModel();
                ReflectionHelper.CopyObject(m, model);
            }
            var ret = BLLObj.Del(m);
            if (ret)
            {
                Dgv.DelDataSource(model);
            }
            Owner.ShowMessage(ret.msg);
        }

        protected virtual void Dgv_DoubleClick(object sender, EventArgs e)
        {
            BtnEdit_Click(sender, e);
        }
    }

    public class FormQuery<TArgPageData, TListModel>
        where TListModel : class, new()
        where TArgPageData : ArgPageData, new()
    {
        public TArgPageData SearchArg { get; } = new TArgPageData();
        /// <summary>
        /// 当绑定(查询)数据源之前触发
        /// </summary>
        public Action<TArgPageData> OnSearchBefore { set; get; }
        /// <summary>
        /// 当搜索完绑定数据源之后触发
        /// </summary>
        public Action<object> OnSearchAfter { set; get; }

        public ToolStripButton BtnADSearch { set; get; }

        public ToolStripButton BtnSearch { set; get; }

        public ToolStripTextBox TxtSearch { set; get; }

        public UcPageNav PageNav { set; get; }

        public GridView Dgv { set; get; }

        public FrmBase Owner { set; get; }

        public Func<TArgPageData, ApiResultList<TListModel>> GetListHandle { set; get; }

        public virtual void Init()
        {
            if (BtnSearch != null) BtnSearch.Click += BtnSearch_Click;
            if (BtnADSearch != null) BtnADSearch.Click += AdSearch_Click;
            if (TxtSearch != null) TxtSearch.KeyDown += TxtSearch_KeyDown;
            if (PageNav != null) PageNav.PageChangedEvent += Page_PageChangedEvent;
        }

        /// <summary>
        /// 绑定数据源
        /// </summary>
        public void RefreshData()
        {
            //分页查询
            Page_PageChangedEvent(1, PageNav?.OnePageCount ?? 0, out int rowCount);
            PageNav.InitiControl(rowCount);
        }

        public virtual void AdSearch_Click(object sender, EventArgs e)
        {
            Owner.ADSearch(SearchArg, RefreshData);
        }
        /// <summary>
        /// 刷新数据源
        /// </summary>
        public virtual void BtnSearch_Click(object sender, EventArgs e)
        {
            SearchArg.ClearADList();
            RefreshData();
        }

        public void TxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshData();
            }
        }

        private void Page_PageChangedEvent(int pageNo, int pageSize, out int rowCount)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = TxtSearch?.Text;
            OnSearchBefore?.Invoke(SearchArg);
            var r = GetListHandle(SearchArg);
            Dgv.DataSource = r.data;
            rowCount = r.count;
            OnSearchAfter?.Invoke(this);
        }
    }
}